package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.DepartmentManagement;
import com.example.prison.entity.PeopleManage;
import com.example.prison.entity.SpecialInfo;
import com.example.prison.mapper.DepartmentManagementMapper;
import com.example.prison.mapper.PeopleManageMapper;
import com.example.prison.service.SpecialInfoService;
import com.example.prison.mapper.SpecialInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author jiang
* @description 针对表【special_Info】的数据库操作Service实现
* @createDate 2024-07-09 11:25:05
*/
@Service
public class SpecialInfoServiceImpl extends ServiceImpl<SpecialInfoMapper, SpecialInfo>
    implements SpecialInfoService{

    @Autowired
    private SpecialInfoMapper specialInfoMapper;

    @Autowired
    private PeopleManageMapper peopleManageMapper;

    @Autowired
    private DepartmentManagementMapper departmentManagementMapper;

    @Override
    public PageInfo<SpecialInfo> get(int pageNumber, int pageSize, String specialPost) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(specialInfoMapper.get(specialPost));
    }

    @Override
    public List<SpecialInfo> getAll() {
        return specialInfoMapper.list();
    }

    @Override
    public int add(SpecialInfo data) {
        if (ObjectUtils.isNotEmpty(specialInfoMapper.getByPost(data.getSpecialPost()))) {
            return -1;
        } else {
            specialInfoMapper.add(data);
            return 1;
        }
    }

    @Override
    public void deleting(Integer[] ids) {
        specialInfoMapper.deleting(ids);
    }

    @Override
    public int updating(SpecialInfo data, Integer id) {
        specialInfoMapper.updating(data, id);
        return 1;
    }

    @Override
    public SpecialInfo getById(Integer deviceId) {
        return specialInfoMapper.getById(deviceId);
    }

    @Override
    public String checkSpecialPost() {
        String message = "";
        List<SpecialInfo> specialList = specialInfoMapper.list();
        Map<String, List<PeopleManage>> map = new HashMap<>();
        List<PeopleManage> peoples = new ArrayList<>();

        for (SpecialInfo special : specialList) {
            String post = special.getSpecialPost();
            String mix = special.getRequestNumber();
            List<DepartmentManagement> list = departmentManagementMapper.get(null, null);
            for (DepartmentManagement depart : list) {
                int number = 0;
                peoples = peopleManageMapper.getListByUnit(depart.getDepartName());
                for (PeopleManage p : peoples) {
                    if (Objects.equals(p.getPost(), post)) {
                        number++;
                    }
                }
                //map.put(depart.getDepartName(), peoples);
                if (number < Integer.parseInt(mix)) {
                    if(ObjectUtils.isEmpty(message) || message.contains(";")) {
                        message += depart.getDepartName();
                    } else {
                        message += ", " + depart.getDepartName();
                    }
                }
            }
            if(!message.equals("")) {
                message = message + post + "职位的人员不足" + mix + "; ";
            }
        }
        return message;
    }

}




